package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.CashWallet;
import cn.net.shuxing.yifu.common.bean.RechargeHistory;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserCashHistory;
import cn.net.shuxing.yifu.common.constant.PaymentStatus;
import cn.net.shuxing.yifu.common.dto.WalletCashDealHistoryDto;
import cn.net.shuxing.yifu.common.mapper.CashWalletMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CashWalletService extends BaseService<CashWallet, CashWalletMapper> {
    @Autowired
    private RechargeHistoryService rechargeHistoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCashHistoryService userCashHistoryService;

    public void insert(Integer userId, BigDecimal cashCount) {
        CashWallet cashWallet = new CashWallet();
        cashWallet.setCashCount(cashCount);
        cashWallet.setId(userId);
        insert(cashWallet);
    }

    public void addCash(Integer userId, BigDecimal cashCount, Integer dealType, String orderKey) {
        CashWallet cashWallet = getByPrimaryKey(userId);
        BigDecimal oldCashCount = cashWallet.getCashCount();
        BigDecimal newCashCount = cashCount.add(oldCashCount);
        cashWallet.setCashCount(newCashCount);
        update(cashWallet);
        UserCashHistory userCashHistory = new UserCashHistory();
        userCashHistory.setCount(cashCount);
        userCashHistory.setUserId(userId);
        userCashHistory.setType(dealType);
        userCashHistory.setOrderKey(orderKey);
        userCashHistory.setUserType(userService.getByPrimaryKeyWithoutComplete(userId).getType());
        userCashHistoryService.insert(userCashHistory);
    }

    public void reduceCash(Integer memeberId, BigDecimal cashCount, Integer dealType, String orderKey) {
        cashCount = new BigDecimal(0).subtract(cashCount);
        addCash(memeberId, cashCount, dealType, orderKey);
    }


    public List<CashWallet> getsByUserType(Integer userType) {
        // TODO: 2019-04-16 用户多重身份
        List<User> all = userService.getsByUserType(userType);
        ArrayList<CashWallet> cashWallets = new ArrayList<>();
        for (User user : all) {
            CashWallet cashWallet = new CashWallet();
            cashWallet.clearCreateTimeAndUpdateTime();
            cashWallet.setId(user.getId());
            cashWallets.add(get(cashWallet));
        }
        return cashWallets;
    }

    public BigDecimal getTotalWithdrawalCash(Integer userType) {
        List<CashWallet> cashWallets = getsByUserType(userType);
        BigDecimal totalWithdrawalCash = new BigDecimal(0);
        for (CashWallet cashWallet : cashWallets) {
            BigDecimal cashCount = cashWallet.getCashCount();
            totalWithdrawalCash = totalWithdrawalCash.add(cashCount);
        }

        return totalWithdrawalCash;
    }


    public CashWallet getByUserId(Integer userId) {
        CashWallet cashWallet = new CashWallet();
        cashWallet.setId(userId);
        cashWallet.clearCreateTimeAndUpdateTime();
        return get(cashWallet);
    }

    @Override
    public int update(CashWallet bean) {
        System.out.println(LocalDateTime.now().toString() + "---> 现金钱包余额即将更新");
        if (super.update(bean) < 1) {
            System.out.println(LocalDateTime.now().toString() + "---> 现金钱包余额更新失败");
            return -1;
        } else {
            System.out.println(LocalDateTime.now().toString() + "---> 现金钱包余额更新完毕");
            return 1;
        }
    }

    public List<WalletCashDealHistoryDto> getWalletCashDealHistory(Integer userId,
                                                                   Integer pageNum,
                                                                   Integer pageSize) {
        List<RechargeHistory> rechargeHistories = rechargeHistoryService.getsWalletCashHistory(userId, PaymentStatus.PAID, pageNum, pageSize);
        ArrayList<WalletCashDealHistoryDto> list = new ArrayList<>();
        if (rechargeHistories != null || !rechargeHistories.isEmpty()) {
            for (RechargeHistory rechargeHistory : rechargeHistories) {
                WalletCashDealHistoryDto listDto = new WalletCashDealHistoryDto();
                listDto.setCreateTime(rechargeHistory.getCreateTime());
                listDto.setDealCount(new BigDecimal(rechargeHistory.getFee()));
                listDto.setDealTypeName("充值");
                list.add(listDto);
            }
        }
        return list;
    }
//    List<RechargeHistory> rechargeHistories = rechargeHistoryService.getsByUserIdAndPaymentStatus(userId, PaymentStatus.PAID);
//        if (rechargeHistories != null) {
//        for (RechargeHistory rechargeHistory : rechargeHistories) {
//            cashDealRecordDtos.add(rechargeHistory.toCashDealRecordDto());
//        }
//    }
}
